//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using System.Windows.Data;
using OpenLS.Drawing;
using System.Windows.Media;

namespace OpenLS.Chart.UI
{
    class LineDashPropertiesToStrokeDashArrayConverter: IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return null;
            if (value is PresetLineDashProperties)
            {
                var p = (PresetLineDashProperties) value;
                var array = GetArray(p.Value);
                if (array == null)
                    return null;
                return new DoubleCollection(array);
            }
            if (value is CustomDashProperties)
            {
                var customDashProperties = (CustomDashProperties) value;
                var list = new List<double>();
                foreach (var stop in customDashProperties.Stops)
                {
                    list.Add(stop.Length);
                    list.Add(stop.SpaceLength);
                }
                return new DoubleCollection(list.ToArray());
            }
            throw new NotSupportedException();
        }

        private static double [] GetArray(PresetLineDashValue presetLineDashValue)
        {
            switch (presetLineDashValue)
            {
                case PresetLineDashValue.Dash:
                    return new[] { 4.0,4 };
                case PresetLineDashValue.DashDot:
                    return new[] { 4.0,4,1,4 };
                case PresetLineDashValue.Solid:
                    return null;
                case PresetLineDashValue.Dot:
                    return new[] { 1.0, 1.0 };
                case PresetLineDashValue.LargeDash:
                    return new[] { 7.0,4};
                case PresetLineDashValue.LargeDashDot:
                    return new[] { 7.0, 4, 1, 4 };
                    case PresetLineDashValue.LargeDashDotDot:
                    return new[] { 7.0, 4, 1, 4, 1, 4 };
                    case PresetLineDashValue.SystemDash:
                    return new[] { 3.0,2 };
                case PresetLineDashValue.SystemDashDot:
                    return new[] { 3.0,2,1,2 };
                    case PresetLineDashValue.SystemDashDotDot:
                    return new [] { 6.0, 4, 1,4, 1,4 };
                    case PresetLineDashValue.SystemDot:
                    return new []{1.0 ,1};

            }
            throw new NotImplementedException();
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    } //\\ duplicate
}
